package com.tydic.system.controller.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.tydic.common.core.domain.R;
import com.tydic.common.core.domain.model.LoginUser;
import com.tydic.common.core.enums.MgrType;
import com.tydic.common.core.utils.StringUtils;
import com.tydic.common.mybatis.core.page.TableDataInfo;
import com.tydic.common.satoken.utils.LoginHelper;
import com.tydic.common.web.core.BaseController;
import com.tydic.system.domain.SysUser;
import com.tydic.system.domain.bo.QuerySysUserBo;
import com.tydic.system.domain.bo.SysUserBo;
import com.tydic.system.domain.vo.SysUserVo;
import com.tydic.system.service.*;
import com.tydic.system.utils.PasswordUtils;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 用户信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/portal/user")
public class SysUserController extends BaseController {

    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysTenantService tenantService;

    /**
     * 获取用户列表
     */
    @PostMapping("/listData")
    public R<TableDataInfo<SysUserVo>> list(QuerySysUserBo bo) {
        return R.ok(TableDataInfo.build(userService.selectPageUserList(bo)));
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/get")
    public R<SysUserVo> get(String userCode) {
        return R.ok(userService.getById(userCode));
    }

    /**
     * 保存用户
     *
     * @param paramRequest
     * @param request
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public R<Void> saveOrUpdate(@RequestBody SysUserBo paramRequest) {
        LoginUser currentUser = LoginHelper.getLoginUser();
        SysUser userEntity = new SysUser();
        if (StringUtils.isNotBlank(paramRequest.getUserCode())) {
            userEntity = userService.getById(paramRequest.getUserCode());
            BeanUtils.copyProperties(paramRequest, userEntity, "login_code", "password");
            if (StrUtil.isNotBlank(paramRequest.getPassword())) {
                userEntity.setPassword(PasswordUtils.encryptPassword(paramRequest.getPassword()));
            }
            userService.saveUser(userEntity, paramRequest.getRoleCodes());
            return R.ok("更新用户信息成功");
        }
        SysUser user = userService.getUserByLoginCode(paramRequest.getLoginCode());
        if (!Objects.isNull(user)) {
            return R.fail("账号已存在!");
        }
        BeanUtils.copyProperties(paramRequest, userEntity);
        userEntity.setUserCode(IdUtil.getSnowflakeNextIdStr());
        userEntity.setMgrType(MgrType.NOT_ADMIN.getValue());
        userEntity.setPassword(PasswordUtils.encryptPassword(paramRequest.getPassword()));
        userService.saveUser(userEntity, paramRequest.getRoleCodes());
        return R.ok("保存用户成功");
    }


    /**
     * 删除用户
     *
     * @param userCode
     * @return
     */
    @GetMapping(value = "/delete")
    public R<Void> delete(@NotBlank(message = "用户编码不能为空") String userCode) {
        boolean result = userService.deleteByUserCode(userCode);
        return result ? R.ok("删除成功") : R.fail("删除失败");
    }
}
